﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace OakEngine.DebugTools
{
   public class OAK_DrawRectangle
    {
       OAK_DrawLine[] m_RectangleLines;
       Rectangle m_Rec;
       public Rectangle Bounds { get { return m_Rec; } set { m_Rec = value; } }
       Vector2[] m_vecAreas;
       Vector2 m_vecPosition;

       int m_nWidth;
       int m_nHeight;


       public Vector2 SetPosition
       {
           get
           {
               return m_vecPosition;
           }
           set
           {
               m_vecPosition = value;
           }
       }

       public OAK_DrawRectangle(GraphicsDevice device)
       {
           m_RectangleLines = new OAK_DrawLine[4];
           m_vecPosition = new Vector2();
           m_Rec = new Rectangle();
           int rectSides = 4;
           for (int i = 0; i < rectSides; i++)
           {
               m_RectangleLines[i] = new OAK_DrawLine(device);
           }

           m_vecAreas = new Vector2[rectSides];
       }

       public OAK_DrawRectangle(ref Texture2D texture) 
       {
           m_RectangleLines = new OAK_DrawLine[4];
           m_vecPosition = new Vector2();
           m_Rec = new Rectangle();
           int rectSides = 4;
           for (int i = 0; i < rectSides; i++)
           {
               m_RectangleLines[i] = new OAK_DrawLine(ref texture);
           }
           m_vecAreas = new Vector2[rectSides];
       }


       public OAK_DrawRectangle(GraphicsDevice device, Color[] color = null)
       {
           m_vecPosition = new Vector2();
           int rectSides = 4;

           m_Rec = new Rectangle();
           m_RectangleLines = new OAK_DrawLine[rectSides];
           for (int i = 0; i < rectSides; i++)
           {
               m_RectangleLines[i] = new OAK_DrawLine(device,color);
               
           }
           m_vecAreas = new Vector2[rectSides];

           
       }

       public void ApplyTextureToRect(Rectangle textureRect)
       {
           SetRectangle(new Vector2(textureRect.X, textureRect.Y),
                        new Vector2(textureRect.Width, textureRect.Y),
                        new Vector2(textureRect.X, textureRect.Height),
                        new Vector2(textureRect.Width, textureRect.Height));

           m_nWidth = textureRect.Width;
           m_nHeight = textureRect.Height;


       }

       public void BuildRect(Rectangle rect)
       {
           SetRectangle(new Vector2(rect.X, rect.Y),
             new Vector2(rect.Width, rect.Y),
             new Vector2(rect.X, rect.Height),
             new Vector2(rect.Width, rect.Height));
       }


       public void ApplyRectToMouse(Vector2 pos,int width = 15, int height = 15)
       {
           // Bounds
           m_Rec.X = (int)pos.X;
           m_Rec.Y = (int)pos.Y;
           m_Rec.Width = width;
           m_Rec.Height = height;
           m_nWidth = width;
           m_nHeight = height;
           Rectangle r = new Rectangle((int)pos.X, (int)pos.Y,(int)pos.X + width,(int)pos.Y + height);
           BuildRect(r);

       }

       public void ApplyRectToMouse(Rectangle pos, int width, int height)
       {
           // Bounds
           m_Rec.X = (int)pos.X;
           m_Rec.Y = (int)pos.Y;
           m_Rec.Width = width;
           m_Rec.Height = height;
           m_nWidth = width;
           m_nHeight = height;
           Rectangle r = new Rectangle((int)pos.X, (int)pos.Y, (int)pos.X + width, (int)pos.Y + height);
           BuildRect(r);

       }

       public void SetRectangle(Vector2 startTop, Vector2 endTop, Vector2 StartBot, Vector2 endBot)
       {
           m_vecAreas[0] = startTop;
           m_vecAreas[1] = endTop;
           m_vecAreas[2] = StartBot;
           m_vecAreas[3] = endBot;


           // TOP Line
           m_RectangleLines[0].UpdateLinePosition(m_vecAreas[0], m_vecAreas[1]);

           // LEft Line
           m_RectangleLines[1].UpdateLinePosition(m_vecAreas[0], m_vecAreas[2]);

           // Bot Line
           m_RectangleLines[2].UpdateLinePosition(m_vecAreas[2], m_vecAreas[3]);

           // Right Line
           m_RectangleLines[3].UpdateLinePosition(m_vecAreas[1], m_vecAreas[3]);
           
       }


       public void SetRectangle(Vector2[] vec)
       {
           m_vecAreas = vec;

           // TOP Line
           m_RectangleLines[0].UpdateLinePosition(m_vecAreas[0], m_vecAreas[1]);

           // LEft Line
           m_RectangleLines[1].UpdateLinePosition(m_vecAreas[0], m_vecAreas[2]);

           // Bot Line
           m_RectangleLines[2].UpdateLinePosition(m_vecAreas[2], m_vecAreas[3]);

           // Right Line
           m_RectangleLines[3].UpdateLinePosition(m_vecAreas[1], m_vecAreas[3]);

       }

       public void UpdatePositionOfCurrentRectangle(Point p)
       {
           Rectangle rect = new Rectangle(p.X,p.Y,p.X+m_nWidth,p.Y +m_nHeight);
           m_vecPosition.X = p.X;
           m_vecPosition.Y = p.Y;
           BuildRect(rect);
       }

       public void UpdatePositionOfCurrentRectangle(Vector2 p)
       {
           
           Rectangle rect = new Rectangle((int)p.X,(int) p.Y,(int) p.X + m_nWidth,(int) p.Y + m_nHeight);
           m_vecPosition.X = p.X;
           m_vecPosition.Y = p.Y;
           BuildRect(rect);
       }

       

       public void UpdatePositionOfCurrentRectangle(Rectangle p)
       {

           m_vecPosition.X = p.X;
           m_vecPosition.Y = p.Y;
           BuildRect(p);
       }


       public void SetLineThickness(int thickness)
       {
           for (int i = 0; i < 4; i++)
           {
               m_RectangleLines[i].LineThickness = thickness;
           }
       }

       public void DrawRect(SpriteBatch batch)
       {
           for (int i = 0; i < m_RectangleLines.Length; i++)
           {
               m_RectangleLines[i].DrawLine(batch);
           }
       }
    }
}
